package com.demo.security.custom.handler;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import com.demo.security.model.Result;
import com.demo.security.utils.ResponseUtils;

@Component
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    
    private Logger log = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        if (authentication != null) {
            log.info("用户[{}]于[{}]注销成功!", ((UserDetails) authentication.getPrincipal()).getUsername(), new Date());
        }
        ResponseUtils.writeJson(response, Result.ok("注销成功"));
    }

}
